<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>提交订单</title>
		<meta http-equiv="pragma" content="no-cache"> 
	    <meta http-equiv="Cache-Control" content="no-cache, must-revalidate"> 
	    <meta http-equiv="expires" content="0">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
		<link rel="stylesheet" href="../css/mui.min.css" />
		<link rel="stylesheet" href="../css/common.css" />
	</head>
	<style type="text/css">
		* {
			margin: 0px;
			padding: 0px;
		}
		body {
			background-color: #F3F3F3;
		}
		.textarea-div {
			position: relative;
		}
		textarea {
			width: 96%;
			margin-left: 2%;
			margin-top: 12px;
			height: 150px;
			border-radius:8px ;
			font-size: 14px;
			color: #B7B7B7;
		}
		.editor-img {
			position: absolute;
		    left: 48%;
		    top: 66px;
		    width: 27px;
		    height: 23px;
		}
		.shopping{clear:both;overflow:hidden;height:auto;padding-bottom: 60px;background-color: #F3F3F3;}
		
		.shopping .addr-div{position: relative; width: 96%; margin-left: 2%; padding: 15px; background-color: #FFFFFF; border-radius: 6px;}
		.shopping .addr-tips-div {display: none; font-size: 13px; color: #838383; font-family: "PingFang-SC-Regular"; line-height: 40px;}
		.shopping .addr-name{font-size: 13px; color: #838383; font-family: "PingFang-SC-Regular";}
		.shopping .addr-phone{margin-left: 30px; font-size: 13px; color: #838383; font-family: "PingFang-SC-Regular";}
		.shopping .addr-info{font-size: 13px; color: #838383; font-family: "PingFang-SC-Regular";}
		.tochoose-div {position: absolute; right: 20px; top: 33%;}
		.tochoose-div .arrow-icon {width: 11px; height: 18px;}
		
	    .div-li {position: relative; border: 2px solid #F3F3F3;}
		.staff-info {width: 96%;margin-left: 2%;border-radius: 6px; height: 90px; background-color: #FFFFFF; position: relative;}
		.staff-info .goods-image-div {display: inline-block; margin-top: 14px; margin-left: 16px;}
		.staff-info .goods-image-div .goods-image {width: 63px !important; height: 63px;}
		.staff-info .goods-info {position: absolute; left: 95px; top: 24px;}
		.staff-info .goods-info .goods-name {font-size: 17px; color: #838383; font-family: "PingFang-SC-Regular";}
		.staff-info .goods-info .goods-specifications {font-size: 13px; color: #838383; font-family: "PingFang-SC-Regular";}
		.staff-info .goods-info .goods-price {font-size: 13px; color: #838383; font-family: "PingFang-SC-Regular";}
		.staff-info .goods-info .goods-price span{color: #EC6B44;}
		.staff-info .review-info-div {display: inline-block; float: right; margin-top: 24px; margin-right: 16px;}
		.staff-info .review-info-div .review-ratio {font-size: 15px; color: #EC6941; font-family: "PingFang-SC-Regular";}
		.staff-info .review-info-div .review-ratio span{color: #EC6941; font-size: 10px;}
		.staff-info .review-info-div .review-num {text-align: right; font-size: 10px; color: #838383; font-family: "PingFang-SC-Regular";}
		
		.payment-bar{clear:both;overflow:hidden;width:100%;height:49px;position:fixed;bottom:0;border-top:1px solid #e0e0e0;background:#fff;}
		.payment-bar .all-checkbox{float:left; width: 88px; height: 49px;}
		.payment-bar .all-checkbox .car-back-div{display: inline-block; width: 80px; height: 49px; background-color: #FFFFFF;background-image: none;}
		.payment-bar .all-checkbox .line-div{display: inline-block; width: 1px; height: 49px; background-color: #C5C5C5;}
		.payment-bar .total-num{display: inline-block; margin:14px 0px 9px 15px; font-size: 15px; color: #818181; font-family: "PingFang-SC-Regular";}
		.payment-bar .shop-total{display: block; float:right;-webkit-box-flex:1.0;box-flex:1.0;margin:14px 20px 9px 0px;}
		.payment-bar .shop-total div{display:block;font-size:13px; color: #737373; font-family: "PingFang-SC-Regular";}
		.payment-bar .settlement{display:inline-block;float:right;width:100px;height:49px;line-height:49px;text-align:center;color:#fff;font-family: "PingFang-SC-Regular"; font-size:15px;background:#00386B;}
		.car-back-div {
			text-align: center;
		}
	</style>
	<body>
		<div class="shopping">
			<div class="addr-div">
				<div class="addr-container">
					<div>
						<span class="addr-name"></span>
						<span class="addr-phone"></span>
					</div>
					<div class="addr-info"></div>
				</div>
				<div class="addr-tips-div">暂无地址，点击去添加！</div>
				<div class="tochoose-div">
					<img class="arrow-icon" src="../img/goods/go_icon.png" />
				</div>
			</div>
			<div class="shop-group-item">
				<ul>
				</ul>
			</div>
			<div class="textarea-div">
				<textarea placeholder="买家留言："></textarea>
				<img class="editor-img goods" src="../img/address/editor.png" />
			</div>
		</div>
		<div class="payment-bar">
			<div class="all-checkbox">
				<div class="car-back-div"><a class="mui-action-back" onclick="javascript:history.back(-1)"><img style="width: 10px; margin-bottom: 15px;" src="../img/common/back_icon.png" /></a></div>
				<div class="line-div"></div>
			</div>
			<div class="total-num"></div>
			<div class="settlement">提交订单</div>
			<div class="shop-total">
				<div>¥<i class="total" id="AllTotal">0.00</i></div>
			</div>
		</div>
	</body>
	<!--<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>-->
	<script type="text/javascript" src="../js/thirdparty/wxjssdk.js" ></script>
	<script type="text/javascript" src="../js/thirdparty/mui.min.js" ></script>
	<script type="text/javascript" src="../js/thirdparty/jquery-3.3.1.js" ></script>
	<script type="text/javascript" src="../js/thirdparty/spin.min.js" ></script>
	<script type="text/javascript" src="../js/thirdparty/swiper.min.js" ></script>
	<script type="text/javascript" src="../js/utils/boot.js?version=1" ></script>
	<script>
		var userID = base.getParameter("userID");
		var merchantID = base.getParameter("merchantID");
		var totalPrice = base.getParameter("totalPrice");
		var totalNum = base.getParameter("totalNum");
		var isSeckill = base.getParameter("isSeckill");
		var seckillID = base.getParameter("seckillID");
		var tempGoodsStandardStr = localStorage.getItem("goodsStandardList");
		var isCorrect = true; //用于判断传过来的数据是否正确 默认为正确的
		var addressID = "";
	    $(function(){
	    		iosPageRefresh(); 
	    		getAddressInfo();//获取默认收货地址
			if (tempGoodsStandardStr) {//取出存储的数据填充到页面中
				var goodsStandardList = eval('(' + tempGoodsStandardStr + ')');
				var html = '';
				var tempTotalPrice = 0;
				for (var index in goodsStandardList) {
					var total = parseInt(goodsStandardList[index].number) * parseFloat(goodsStandardList[index].price); //计算单个商品的总价
		          	tempTotalPrice += total; //计算该购物车的总价
					html += getListItem(goodsStandardList[index], index);
				}
				if (goodsStandardList.length > 0) {
					if (tempTotalPrice == totalPrice) {
						$("#AllTotal").text(totalPrice);
						$(".total-num").text("共"+totalNum+"件");
					} else {
						isCorrect = false;
						mui.toast("数据错误，请重新结算！");
						setTimeout(function() {
							mui.back();
						}, 1000)
					}
				}
				$(".shop-group-item ul").html(html);
			} else {
				isCorrect = false;
				mui.toast("数据错误，请重新结算！");
				setTimeout(function() {
					mui.back();
				}, 1000)
			}
		});
		function getAddressInfo () {
			var url = base.url.getAddressInfo;
			var data = {
				userID: userID,
				addressID: ""
			}
			base.postData(url,data,getAddressInfoSuccess);
		}
		function getAddressInfoSuccess(data) {
			if (data.success) {
				if(data.context.address.addressID) {
					addressID = data.context.address.addressID;
					$(".addr-name").text(data.context.address.consigneeName);
					$(".addr-phone").text(data.context.address.consigneePhone);
					$(".addr-info").text(data.context.address.completeAddress);
				} else {
					$(".addr-container").hide();
					$(".addr-tips-div").css("display", "inline-block");
				}
			} else {
				mui.toast(data.msg);
			}
		}
		function getListItem(item,index) {
			var html = '';
			html += '<li class="div-li" data-index="' + index + '">'
			html += '	<div class="staff-info">'
			html += '		<div class="goods-image-div"><img class="goods-image" src="'+item.goodsImg+'" /></div>'
			html += '		<div class="goods-info">'
			html += '			<div class="goods-name">'+item.goodsName+'</div>'
			html += '			<div class="goods-price">'+item.standard+'</div>'
			html += '		</div>'
			html += '		<div class="review-info-div">'
			html += '			<div class="review-ratio"><span>¥</span>'+item.price+'</div>'
			html += '			<div class="review-num">x'+item.number+'</div>'
			html += '		</div>'
			html += '	</div>'
			html += '</li>'
			return html;
		}
		$(".shopping").on("tap", ".addr-div", function() {
			window.location.href = "addressManagement.html?userID="+userID;
		})
		$(".payment-bar").on("tap", ".settlement", function() {
			if (isCorrect) {
				if (tempGoodsStandardStr) {//取出存储的数据填充到页面中
//					var goodsStandardList = eval('(' + tempGoodsStandardStr + ')');
					placeOrder(tempGoodsStandardStr);//提交订单
				}
			} else {
				mui.toast("数据错误，请重新结算！")
			}
		})
		function placeOrder (goodsStandardList) {
			if (addressID == "" || addressID == null) {
				mui.toast("请选择您的收货地址！");
				return;
			}
			if (isSeckill == 1) {
				var url = base.url.buySeckillGooods;
				var goodsStandard = eval('(' + goodsStandardList + ')');
				var data = {
					userID: userID,
					addressID: addressID,
					merchantID: merchantID,
					seckillID:seckillID,
					goodsID:goodsStandard[0].goodsID,
					number:goodsStandard[0].number,
					goodsStandardList: goodsStandardList,
					remark: $("textarea").text(),
					totalPrice:$("#AllTotal").text()
				}
				base.postData(url,data,placeOrderSuccess);
			}else{
				var url = base.url.placeOrder;
				var data = {
					userID: userID,
					addressID: addressID,
					merchantID: merchantID,
					goodsStandardList: goodsStandardList,
					remark: $("textarea").text(),
					totalPrice:$("#AllTotal").text()
				}
				base.postData(url,data,placeOrderSuccess);
			}
			
		}
		function placeOrderSuccess(data) {
			if (data.success) {
				window.localStorage.removeItem("goodsStandardList"); //清除缓存的下订单数据
				mui.toast(data.msg);
				if (data.context.isChooseStore == 1) {  //如果需要选择门店 那么跳转到选择门店页面
					window.location.href = "stores.html?userID="+userID+"&orderNo="+data.context.orderNo;
				} else {  //如果不需要选择门店 那么直接支付
					base.weChatPay(data.context.orderNo, userID, function(rse) {
						//判断是否是在wx小程序环境  
						if(window.__wxjs_environment === 'miniprogram'){  
							//点击微信支付后，调取统一下单接口生成微信小程序支付需要的支付参数  
							var params = '?timestamp='+rse.timeStamp+'&nonceStr='+rse.nonceStr  
							             +'&package='+rse.package+'&paySign='+rse.sign+'&orderNo='+data.context.orderNo;  
							//定义path 与小程序的支付页面的路径相对应  
							var path = '/pages/wxpay/wxpay'+params;  
							//通过JSSDK的api使小程序跳转到指定的小程序页面  
							wx.miniProgram.navigateTo({url: path});      
						}
					});
				}
			} else {
				mui.toast(data.msg);
			}
		}
		function iosPageRefresh () { //ios页面返回当前页时强制刷新
			var isPageHide = false;   
			window.addEventListener('pageshow', function () {      
			    if (isPageHide) {        
			      window.location.reload();      
			    }    
			});    
			window.addEventListener('pagehide', function () {
			    isPageHide = true;    
			});
		}
	  </script>
</html>
